import colorCofig from "@/config/color.js"
import config from "@/config/config.js"
import storage from "@/uni-sub-base/utils/storage.js"
import { setScroll, setNoScroll } from "@/uni-sub-base/utils/elementUtil.js"
import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js"
import MescrollMoreItemMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
import { getPlatform } from "@/uni-sub-base/utils/platformUtil.js"
export default {
    mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
    data() {
        return {
            downOption: {
                use: true,
            },
            upOption: {
                page: {
                    num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
                    size: 10, // 每页数据的数量
                    time: null, // 加载第一页数据服务器返回的时间; 防止用户翻页时,后台新增了数据从而导致下一页数据重复;
                },
                textLoading: "加载中 ...", // 加载中的提示文本
                textNoMore: "-- 没有更多了 --", // 没有更多数据的提示文本
                empty: {
                    use: true, // 是否显示空布局
                    // icon: "/static/img/empty.png", // 图标路径 (建议放入static目录, 如 /static/img/mescroll-empty.png )
                    tip: "~ 暂无数据 ~", // 空提示
                },
                toTop: {
                    src: false,
                },
            },
            emptyOption: {
                tip: "~ 暂无数据 ~", // 空提示
            },
            listHeight: "",
            loadOk: false,
        }
    },
    onLoad(e) {
        //判断登录状态
    },
    onUnload() {},
    methods: {
        //跳转页面
        easyGoTo(path) {
            uni.navigateTo({
                url: path,
            })
        },
        //返回上一页or指定页
        easyGoBack(pageNum = 1) {
            if (pageNum > 1) {
                let pages = getCurrentPages()
                if (pages.length > pageNum) {
                    //回到首页
                    uni.navigateBack({
                        delta: pageNum,
                    })
                }
            } else {
                uni.navigateBack()
            }
        },
        //返回首页
        easyGoHome() {
            let pages = getCurrentPages()
            if (pages.length > 1) {
                //回到首页
                uni.navigateBack({
                    delta: getCurrentPages().length,
                })
            }
        },
        //清除页面历史栈跳转页面
        easyReLaunch(path) {
            uni.reLaunch({
                url: path,
            })
        },
        //清除当前页跳转页面
        easyRemoveCurrentGoTo(path) {
            uni.redirectTo({
                url: path,
            })
        },
        // 拨打电话
        makePhoneCall(phoneNumber) {
            if (phoneNumber) {
                uni.makePhoneCall({
                    phoneNumber: phoneNumber,
                })
            } else {
                this.easyToast("电话号码为空")
            }
        },
        //简单toast弹窗提示
        easyToast(msg) {
            uni.showToast({
                title: msg,
                icon: "none",
                duration: 2000,
            })
        },
        //获取颜色值主要给一些不能直接设置css的组件调用
        easyGetColor(color) {
            if (color == "main") {
                return colorCofig.color.main
            }
            if (color == "orange") {
                return colorCofig.color.orange
            }
            if (color == "yellow") {
                return colorCofig.color.yellow
            }
            if (color == "white") {
                return colorCofig.color.white
            }
            if (color == "red") {
                return colorCofig.color.red
            }
            if (color == "black") {
                return colorCofig.color.black
            }
            return colorCofig.color.green
        },
        //默认头像
        easyGetLogo() {
            return "/static/logo.png"
        },
        //默认头像
        easyGetDefalutAvatar() {
            return "/static/avatar.png"
        },
        //获取图片拼接全地址
        easyGetImgUrl(url) {
			let ImageDomain = storage.getCache("ImageDomain")
            if (!url || url == "") {
                return "/static/img/no-img.jpg"
            }
            if (url.includes("http")) {
                //全路径直接返回自己
                return url
            }
            return ImageDomain?ImageDomain + url:config.baseImgUrl + url
        },
        easyGetImgListUrl(url) {
            if (!url || url == "") {
                return this.easyGetImgUrl(url)
            } else if (url.indexOf(",") != -1) {
                let urlArr = url.split(",")
                return this.easyGetImgUrl(urlArr[0])
            } else {
                return this.easyGetImgUrl(url)
            }
        },
        easyImgList(item) {
            let arr = []
            if (item.indexOf(",") != -1) {
                let urlArr = item.split(",")
                urlArr.forEach((item) => {
                    arr.push(this.easyGetImgListUrl(item))
                })
                return arr
            } else {
                let urlArr = item.split()
                urlArr.forEach((item) => {
                    arr.push(this.easyGetImgListUrl(item))
                })
                return arr
            }
        },
        // 滚动、禁止滚动
        setScroll() {
            setScroll()
        },
        setNoScroll() {
            setNoScroll()
        },
        easyWebViewGoBack(webView) {
            webView.evalJS("javascript:history.back(-1)")
        },
        easyGetListHeight(element) {
            //获取商品高度
            getElementHeight(this, element, (e) => {
                this.listHeight = e + "px"
                this.loadOk = true
            })
        },
        formateSeconds(endTime) {
            let secondTime = parseInt(endTime) //将传入的秒的值转化为Number
            let min = 0 // 初始化分
            let h = 0 // 初始化小时
            let result = ""
            if (secondTime > 60) {
                //如果秒数大于60，将秒数转换成整数
                min = parseInt(secondTime / 60) //获取分钟，除以60取整数，得到整数分钟
                secondTime = parseInt(secondTime % 60) //获取秒数，秒数取佘，得到整数秒数
                if (min > 60) {
                    //如果分钟大于60，将分钟转换成小时
                    h = parseInt(min / 60) //获取小时，获取分钟除以60，得到整数小时
                    min = parseInt(min % 60) //获取小时后取佘的分，获取分钟除以60取佘的分
                }
            }
            result = `${h.toString().padStart(2, "0")}:${min.toString().padStart(2, "0")}:${secondTime
                .toString()
                .padStart(2, "0")}`
            return result
        },
        getAppSet() {
            let productDisplay = storage.getCache("productDisplay")
            if (productDisplay) {
                let appSet = config.appSet
                appSet.isHideImg = productDisplay.IsShowImg == 0
                appSet.isHideProDetail = productDisplay.IsShowImg == 0
                appSet.IsShowConversionRate = productDisplay.IsShowConversionRate
                appSet.IsShowRemark = productDisplay.IsShowRemark
                appSet.IsShowSkuList = productDisplay.IsShowSkuList
                appSet.NoCheckStock = productDisplay.NoCheckStock
                appSet.PriceDecimalPlaces = productDisplay.PriceDecimalPlaces
                return appSet
            }
            return config.appSet
        },
        getAppId() {
            let appSet = config.appSet
            if (appSet.appIds) {
                if (getPlatform() == "android") {
                    return appSet.appIds.android ? appSet.appIds.android : ""
                }
                if (getPlatform() == "ios") {
                    return appSet.appIds.ios ? appSet.appIds.ios : ""
                }
            }
            return ""
        },
        getUserInfo() {
            let userInfo = storage.getCache("userInfo")
            return userInfo
        },
        getPayIcon(type) {
            let icon = ""
            switch (type) {
                case 0:
                    icon = "/static/img/bank/icon_pay_type_xx.png"
                    break
                case 1:
                    icon = "/static/img/bank/yepay.png"
                    break
                case 2:
                    icon = "/static/img/bank/icon_pay_type_jh.png"
                    break
                case 4:
                    icon = "/static/img/bank/icon_pay_type_wx.png"
                    break
                case 5:
                    icon = "/static/img/bank/icon_pay_type_zfb.png"
                    break
                case 7:
                    icon = "/static/img/bank/icon_pay_type_fuiou.png"
                    break
                case 8:
                case 12:
                    icon = "/static/img/bank/icon_pay_type_yhk.png"
                    break
                case 10:
                    icon = "/static/img/bank/icon_pay_type_xy.png"
                    break
                default:
                    icon = "/static/img/bank/icon_pay_type_xx.png"
                    break
            }
            return icon
        },
        getOfflinePayTypeIcon(id) {
            switch (id) {
                case 0:
                    return "/static/img/bank/ylpay.png"
                    break
                case 1:
                    return "/static/img/bank/wxpay.png"
                    break
                case 2:
                    return "/static/img/bank/zfbpay.png"
                    break
                case 10:
                    return "/static/img/bank/bank.png"
                    break
                default:
                    return "/static/img/bank/bank.png"
                    break
            }
        },
        getPostText(usePost) {
            let text = ""
            switch (usePost) {
                case 0:
                    text = "商家配送"
                    break
                case 1:
                    text = "线下自提"
                    break
                case 2:
                    text = "暂存公司"
                    break
                case 3:
                    text = "门店交易"
                    break
                case 4:
                    text = "园区采摘"
                    break
                case 5:
                    text = "免配送"
                    break
                default:
                    break
            }
            return text
        },
        openMiniProgram(path,id, appId, type) {
            //#ifdef APP-PLUS
            plus.share.getServices((res) => {
                if (res && res.length > 0) {
                    let sweixin = res.find((e) => {
                        return e.id == "weixin"
                    })
                    //唤醒微信小程序
                    if (sweixin) {
						sweixin.launchMiniProgram({
						    id: id,
						    type: type, //小程序版本  0-正式版； 1-测试版； 2-体验版。
						    path: path,
						})
                    }
                }
            })
            //#endif
            //#ifdef MP-WEIXIN
            uni.navigateToMiniProgram({
                appId: "",
                path: path,
                extraData: {
                    data1: "test",
                },
                success(res) {
                    // 打开成功
                },
            })
            //#endif
        },
    },
}
